/**
 * 使用 fetch 实现 GET 请求
 * @param {string} url - 请求的 URL
 * @param {Object} options - 可选的请求配置
 * @returns {Promise} 返回 Promise 对象
 */
export const get = async (url, options = {}) => {
  try {
    const defaultOptions = {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        ...options.headers
      },
      ...options
    };

    const response = await fetch(url, defaultOptions);
    
    // 检查响应状态
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    // 解析响应数据
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('GET request failed:', error);
    throw error;
  }
};

/**
 * 带参数的 GET 请求
 * @param {string} url - 基础 URL
 * @param {Object} params - 查询参数对象
 * @param {Object} options - 可选的请求配置
 * @returns {Promise} 返回 Promise 对象
 */
export const getWithParams = async (url, params = {}, options = {}) => {
  try {
    // 构建查询字符串
    const queryString = new URLSearchParams(params).toString();
    const fullUrl = queryString ? `${url}?${queryString}` : url;
    
    return await get(fullUrl, options);
  } catch (error) {
    console.error('GET request with params failed:', error);
    throw error;
  }
};

// 默认导出
export default {
  get,
  getWithParams
};
